﻿using Spire.Pdf;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace PdfOperator
{
    public partial class FrmPdfToWord : Form
    {
        public FrmPdfToWord()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 打开PDF
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpenfile_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Title = "打开PDF";
            fileDialog.Filter = "pdf文件|*.pdf";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                string name = fileDialog.FileName;
                txtFilePath.Text = name;
            }
        }

        /// <summary>
        /// 选择保存路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSavePath_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtFilePath.Text))
            {
                MessageBox.Show("请先选择要操作的pdf！");
                return;
            }
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.Title = "选择保存路径";
            saveFile.Filter = "word文件|*.doc;*.docx";
            saveFile.DefaultExt = ".docx";
            saveFile.FileName = "转为word的文档.docx";
            if (saveFile.ShowDialog() == DialogResult.OK)
            {
                txtSavePath.Text = saveFile.FileName;
            }
        }

        /// <summary>
        /// 开始转换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtFilePath.Text))
            {
                MessageBox.Show("请选择要操作的pdf文件！");
                return;
            }
            //初始化PdfDocument实例
            Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument();
            //加载PDF文档
            doc.LoadFromFile(txtFilePath.Text);
            //保存为Doc格式文档
            doc.SaveToFile(txtSavePath.Text, FileFormat.DOCX);
            MessageBox.Show("完成");
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 打开转换后的Word
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpenWord_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!System.IO.File.Exists(txtSavePath.Text))
            {
                return;
            }
            else
            {
                //存在文件
                System.Diagnostics.Process.Start(txtSavePath.Text);
            }
        }


        //开始PDF转Word（调用Python方法）
        private void btnStart2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtFilePath.Text))
            {
                MessageBox.Show("请添加要操作的pdf文档！");
                return;
            }
            if (string.IsNullOrEmpty(txtSavePath.Text))
            {
                MessageBox.Show("请选择转换后的word文件保存路径！");
                return;
            }

            string args = txtFilePath.Text + " " + txtSavePath.Text;
            Console.WriteLine(args);
            if (File.Exists("PdfToWord.exe"))   //文件存在
            {
                Console.WriteLine("文件存在");
                try
                {
                    Process myProcess = new Process();
                    // 调用当前目录下的PdfToWord.exe（Python程序）进行转换
                    ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("PdfToWord.exe", args);
                    myProcessStartInfo.CreateNoWindow = true;
                    myProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    myProcess.StartInfo = myProcessStartInfo;
                    myProcess.Start();

                    while (!myProcess.HasExited)
                    {
                        myProcess.WaitForExit();
                    }

                    _ = myProcess.ExitCode;
                    myProcess.Dispose();
                    myProcess.Close();

                    MessageBox.Show("完成");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
    }
}
